package com.fingard.dsp.bank.directbank.icbc02;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IForeignCurrency;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqQryForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.RetAtsForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.detail.RetAtsForeignCurrPayDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 工行外币付款
 * Created by F on 2018/11/6.
 */
public class ICBC02ForeignCurrSinglePay extends ICBC02Base implements IForeignCurrency {

    private static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,BUSITYPE,TRANSDATE,REQSEQID,RESERVE1) values (?,?,?,?,?,?);";
    private static final String SELECTSEQ = "SELECT RESERVE1 FROM REQSEQIDMAPPING WHERE REQSEQID=?;";

    /**
     * 指令提交
     * @param reqForeignCurrSingle
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsForeignCurrSingle foreignCurrPay(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception{
        //返回ATS的对象
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetDetail;
        if("CrossborderFCP".equals(reqForeignCurrSingle.reqHeader.transType)){//跨境外币
            tmpRetDetail = crossborderFcp(reqForeignCurrSingle);
        }else if("DomesticFCP".equals(reqForeignCurrSingle.reqHeader.transType)){//境内外币
            tmpRetDetail = domesticFcp(reqForeignCurrSingle);
        }else if("OutsideFCP".equals(reqForeignCurrSingle.reqHeader.transType)){//全球汇款
            tmpRetDetail = outsideFcp(reqForeignCurrSingle);
        }else {
            tmpRetDetail = new RetAtsForeignCurrPayDetail();
            tmpRetDetail.respCode = "0";
            tmpRetDetail.respInfo = "无效的指令代码";
            tmpRetDetail.transState = PaymentState.FAIL_Payment;
            tmpRetDetail.payInfoCode = "E1301";
        }
        if(tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)){
            tmpRetDetail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    /**
     * 查询指令状态
     * @param reqQryForeignCurrSingle
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsForeignCurrSingle qryForeignCurrPay(ReqQryForeignCurrSingle reqQryForeignCurrSingle) throws Exception{
        //返回ATS的对象
        RetAtsForeignCurrSingle retAts = new RetAtsForeignCurrSingle(reqQryForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetAtsDetail;
        if("CrossborderFCSQ".equals(reqQryForeignCurrSingle.reqHeader.transType)){//跨境外币汇款状态查询
            tmpRetAtsDetail = payResultCrossborderFCP(reqQryForeignCurrSingle);
        }else if("DomesticFCSQ".equals(reqQryForeignCurrSingle.reqHeader.transType)){//境内外币汇款状态查询
            tmpRetAtsDetail = payResultDomesticFcp(reqQryForeignCurrSingle);
        }else if("OutsideFCSQ".equals(reqQryForeignCurrSingle.reqHeader.transType)){//全球汇款状态查询
            tmpRetAtsDetail = payResultOutsideFcp(reqQryForeignCurrSingle);
        }else {
            tmpRetAtsDetail = new RetAtsForeignCurrPayDetail();
            tmpRetAtsDetail.respCode = "0";
            tmpRetAtsDetail.respInfo = "无效的指令代码";
            tmpRetAtsDetail.transState = PaymentState.FAIL_Payment;
            tmpRetAtsDetail.payInfoCode = "E1301";
        }
        if(tmpRetAtsDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetAtsDetail.reqSeqID)){
            tmpRetAtsDetail.reqSeqID = reqQryForeignCurrSingle.askNumber;
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    /**
     * 跨境外币汇款指令提交
     * @param reqForeignCurrSingle
     * @return
     */
    public RetAtsForeignCurrPayDetail crossborderFcp(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception{
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        Connection conn=FGBiz.getLocalDbConn();
        try {
            bnkPackageID = reqForeignCurrSingle.singleDetail.askNumber;
            bnkTransCode = "KJWHKK";
            bnkfSeqno = bnkPackageID;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入ICBC02通用的头部节点
            writeCommonHeader(xmlWriter);
            String sysIOFlag;
            //写入报文体
            xmlWriter.writeElementString("PayAcctName", reqForeignCurrSingle.singleDetail.payName);//汇款人中文名称
            xmlWriter.writeElementString("PayAcctNo", reqForeignCurrSingle.singleDetail.payAct);//付款账号
            xmlWriter.writeElementString("PayENAcctName", "");//汇款人英文名称
            xmlWriter.writeElementString("PayAddr", reqForeignCurrSingle.singleDetail.payAddr);//汇款人地址
            xmlWriter.writeElementString("Phone", reqForeignCurrSingle.singleDetail.contactNum);//联系电话
            if ("1".equals(reqForeignCurrSingle.singleDetail.sameBnk)) {
                sysIOFlag = "1";
            } else {
                sysIOFlag = "2";
            }
            xmlWriter.writeElementString("SysIOFlag", sysIOFlag);//收款人开户行是否为工行？
            xmlWriter.writeElementString("RecAcctName", reqForeignCurrSingle.singleDetail.recName);//收款人名称
            xmlWriter.writeElementString("RecAcctNo", reqForeignCurrSingle.singleDetail.recAct);//收款人账号
            xmlWriter.writeElementString("RecAddr", reqForeignCurrSingle.singleDetail.recAddr);//收款人地址
            xmlWriter.writeElementString("RecCountry", "Z"+reqForeignCurrSingle.singleDetail.recCountryCode);//收款人常驻国家（地区）及代码
            xmlWriter.writeElementString("RecBankName", reqForeignCurrSingle.singleDetail.recCNAPSName);//收款人开户行名称
            if(!"".equals(reqForeignCurrSingle.singleDetail.recSWIFTCODE) && reqForeignCurrSingle.singleDetail.recSWIFTCODE.length()<=11){
                xmlWriter.writeElementString("RecBankCode", reqForeignCurrSingle.singleDetail.recSWIFTCODE);//收款人开户行代码
            }else if(!"".equals(reqForeignCurrSingle.singleDetail.recSwiftCode) && reqForeignCurrSingle.singleDetail.recSwiftCode.length()<=11){
                xmlWriter.writeElementString("RecBankCode", reqForeignCurrSingle.singleDetail.recSwiftCode);//收款人开户行代码
            }else if(!"".equals(reqForeignCurrSingle.singleDetail.recAreaCode) && reqForeignCurrSingle.singleDetail.recAreaCode.length()<=11){
                //因ats系统推送报文中收款人swiftcode为空，测试暂用银行区域码
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "收款人swiftCode为空";
                return tmpRetDetail;
            }
            xmlWriter.writeElementString("RecBankAddr", "");//收款人开户行地址
            xmlWriter.writeElementString("RecBankCountry", reqForeignCurrSingle.singleDetail.recCountryCode);//收款人开户行所在国家
            xmlWriter.writeElementString("RecBankCity", "");//收款人开户行所在城市
            xmlWriter.writeElementString("RecAgtBankName", reqForeignCurrSingle.singleDetail.recAgentBnkName);//收款银行之代理行名称
            xmlWriter.writeElementString("RecAgeBankAddr", "");//收款银行之代理行地址
            xmlWriter.writeElementString("CurrType", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur));//汇款币种(32A)
            xmlWriter.writeElementString("PayAmt", StringHelper.amountMultiply100(reqForeignCurrSingle.singleDetail.payAmount));//汇款金额(32A)
            xmlWriter.writeElementString("TranNos", reqForeignCurrSingle.singleDetail.transCode1+"|"+reqForeignCurrSingle.singleDetail.transCode2);//交易编码
            xmlWriter.writeElementString("TPostscripts", reqForeignCurrSingle.singleDetail.postScript1+"|"+reqForeignCurrSingle.singleDetail.postScript2);//交易附言
            xmlWriter.writeElementString("TCurrTypes", convertCurToBank(reqForeignCurrSingle.singleDetail.currency1)+"|"
                    +convertCurToBank(reqForeignCurrSingle.singleDetail.currency2));//相应币种
            xmlWriter.writeElementString("TAmts", StringHelper.amountMultiply100(reqForeignCurrSingle.singleDetail.amout1)+"|"
                    +StringHelper.amountMultiply100(reqForeignCurrSingle.singleDetail.amout2));//相应金额
            xmlWriter.writeElementString("Postscript", reqForeignCurrSingle.singleDetail.description);//汇款附言
            xmlWriter.writeElementString("KXProp", "");//款项性质
            xmlWriter.writeElementString("PayProp", getPayPropByMethod(reqForeignCurrSingle.singleDetail.payMethod));//付款性质
            xmlWriter.writeElementString("HXFlag", reqForeignCurrSingle.singleDetail.ispaymentforimportitem);//是否为进口核销项下付款
            String bsFlag="0"; //收款人是否在保税区 0否 1是
            if("X".equals(reqForeignCurrSingle.singleDetail.payProperty)){
                bsFlag="1";
            }
            xmlWriter.writeElementString("BSFlag", bsFlag);//收款人是否在保税区
            xmlWriter.writeElementString("ZYDate", "");//最迟装运日期
            xmlWriter.writeElementString("ContractNo", reqForeignCurrSingle.singleDetail.contractNo);//合同号
            xmlWriter.writeElementString("BillNo", reqForeignCurrSingle.singleDetail.invoiceNo);//发票号
            xmlWriter.writeElementString("BUnitCode", "");//报关单经营单位代码
            xmlWriter.writeElementString("BPageNo", "");//报关单号
            xmlWriter.writeElementString("BCurrType", "");//报关单币种
            xmlWriter.writeElementString("BTotalAmt", "");//报关单总金额
            xmlWriter.writeElementString("HZAmt", "");//本次核注金额
            xmlWriter.writeElementString("FeePayer", reqForeignCurrSingle.singleDetail.costMode);//国内外费用承担
            xmlWriter.writeElementString("FeeAcctNo", reqForeignCurrSingle.singleDetail.feeAct);//汇款费用手续费、邮电费付费账号
            xmlWriter.writeElementString("Remark", reqForeignCurrSingle.singleDetail.memo);//备注
            xmlWriter.writeElementString("FileList", "");//上传文件名
            xmlWriter.writeElementString("IndexList", "");//索引号
            xmlWriter.writeElementString("SignTime", Format.DateTime17SSSFormat.format(new Date()));//签名时间
            xmlWriter.writeElementString("ReqReserved1", "");//请求备用字段1
            xmlWriter.writeElementString("ReqReserved2", "");//请求备用字段2
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            //记录对应流水号的收款人开户行是否为工行，供查询状态接口使用
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            JDBCHelper.update(conn,INSERTSEQ,new Object[]{uuid,reqForeignCurrSingle.reqHeader.bankCode,"sysIOFlag",
                    Format.Date8Format.format(new Date()),reqForeignCurrSingle.singleDetail.askNumber,sysIOFlag});
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),true);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRespCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");//响应码
                    String tmpRetInfo = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");//响应信息
                    if(isActiveRetCode(tmpRespCode)){
                        String result=tmpBkReader.getFirstText("/CMS/eb/out/Result");//指令状态
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        tmpRetDetail.transState = tmpRespCode+"+"+result;
                        tmpRetDetail.payInfoCode = tmpRetDetail.transState;
                        tmpRetDetail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
                    }else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo.toString();
                    }
                }else{
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            }else if(tmpStrRet[0].equals(ConnState.FAIL)){
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1];
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = tmpStrRet[0];
                tmpRetDetail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
            }
            else{
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        }catch (Exception e){
            e.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = e.getMessage();
            WriteBankLogLn("跨境外币指令提交异常："+e);
        }finally {
            conn.close();
        }
        return  tmpRetDetail;
    }

    /**
     * 境内外币汇款指令提交
     * @param reqForeignCurrSingle
     * @return
     */
    public RetAtsForeignCurrPayDetail domesticFcp(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception{
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        try {
            bnkPackageID = reqForeignCurrSingle.singleDetail.askNumber;
            bnkTransCode = "LOCEXCH";
            bnkfSeqno = bnkPackageID;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入ICBC02通用的头部节点
            writeCommonHeader(xmlWriter);
            //写入报文体
            xmlWriter.writeElementString("OnlBatF", "1");//联机批量标志
            xmlWriter.writeElementString("SettleMode", "0");//入账方式  2：并笔记账 0：逐笔记账(目前只支持逐笔记账)
            xmlWriter.writeElementString("TotalNum", "1");//总笔数
            String tmpPayAmt = StringHelper.amountMultiply100(reqForeignCurrSingle.singleDetail.payAmount);
            xmlWriter.writeElementString("TotalAmt", tmpPayAmt);//总金额
            xmlWriter.writeElementString("CurrType", convertCurToBank(reqForeignCurrSingle.singleDetail.payCur));//币种
            xmlWriter.writeElementString("SignTime", Format.DateTime17Format.format(new Date()));//签名时间
            xmlWriter.writeElementString("ReqReserved1", "");//请求备用字段1
            xmlWriter.writeElementString("ReqReserved2", "");//请求备用字段2
            XElement tmpXElement = new XElement("rd", "");
            tmpXElement.addChild(new XElement("iSeqno", "1"));//指令包顺序号
            //PayType 1：加急 2：普通
            if ("1".equals(reqForeignCurrSingle.singleDetail.paySpd)) {
                tmpXElement.addChild(new XElement("PayType", "1"));//记账处理方式
            } else {
                tmpXElement.addChild(new XElement("PayType", "2"));//记账处理方式
            }
            if ("1".equals(reqForeignCurrSingle.singleDetail.sameBnk)) {//同行
                tmpXElement.addChild(new XElement("isICBCAcc", "1"));//收款行标识
            } else {
                tmpXElement.addChild(new XElement("isICBCAcc", "0"));//收款行标识
            }
            tmpXElement.addChild(new XElement("PayAccNo", reqForeignCurrSingle.singleDetail.payAct));//付款账号
            tmpXElement.addChild(new XElement("RecBranch", reqForeignCurrSingle.singleDetail.recCNAPSName));//收款账号开户行
            tmpXElement.addChild(new XElement("RecAreaName", reqForeignCurrSingle.singleDetail.recAreaName));//收款账号地区名称
            tmpXElement.addChild(new XElement("RecAccNo", reqForeignCurrSingle.singleDetail.recAct));//收款账号
            tmpXElement.addChild(new XElement("RecAccNameCN", reqForeignCurrSingle.singleDetail.recName));//收款人户名
            tmpXElement.addChild(new XElement("PayAmt", StringHelper.amountMultiply100(reqForeignCurrSingle.singleDetail.payAmount)));
            tmpXElement.addChild(new XElement("UseCode", ""));
            tmpXElement.addChild(new XElement("UseCN", reqForeignCurrSingle.singleDetail.purpose));
            tmpXElement.addChild(new XElement("EnSummary", ""));
            tmpXElement.addChild(new XElement("PostScript", reqForeignCurrSingle.singleDetail.memo));//附言
            tmpXElement.addChild(new XElement("Summary", reqForeignCurrSingle.singleDetail.description));//摘要
            tmpXElement.addChild(new XElement("Ref", reqForeignCurrSingle.singleDetail.askNumber));//业务编号（业务参考号）
            tmpXElement.addChild(new XElement("Oref", reqForeignCurrSingle.singleDetail.askNumber));//相关业务编号
            tmpXElement.addChild(new XElement("ERPSqn", reqForeignCurrSingle.singleDetail.askNumber));//ERP流水号
            tmpXElement.addChild(new XElement("BusCode", ""));//业务代码
            tmpXElement.addChild(new XElement("ERPcheckno", ""));//ERP支票号
            tmpXElement.addChild(new XElement("CrvouhType", ""));//原始凭证种类
            tmpXElement.addChild(new XElement("CrvouhName", ""));//原始凭证名称
            tmpXElement.addChild(new XElement("CrvouhNo", ""));//原始凭证号
            tmpXElement.addChild(new XElement("ReqReserved3", ""));//行别
            tmpXElement.addChild(new XElement("ReqReserved4", reqForeignCurrSingle.singleDetail.declareFlag));//请求备用字段4
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();//写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),true);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRespCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");//响应码
                    String tmpRetInfo = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");//响应信息
                    if(isActiveRetCode(tmpRespCode)){
                        String result=tmpBkReader.getFirstText("/CMS/eb/out/rd/Result");//指令状态
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        tmpRetDetail.transState = tmpRespCode+"+"+result;
                        tmpRetDetail.payInfoCode = tmpRetDetail.transState;
                        tmpRetDetail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
                    }else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo.toString();
                    }
                }else{
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            }else if(tmpStrRet[0].equals(ConnState.FAIL)){
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1];
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.reqSeqID = reqForeignCurrSingle.singleDetail.askNumber;
                tmpRetDetail.payInfoCode = tmpStrRet[0];
            }
            else{
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }

        }catch (Exception e){
            e.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = e.getMessage();
            WriteBankLogLn("境内外币指令提交异常："+e);
        }
        return  tmpRetDetail;
    }

    /**
     * 全球汇款
     * @param reqForeignCurrSingle
     * @return
     */
    public RetAtsForeignCurrPayDetail outsideFcp(ReqForeignCurrSingle reqForeignCurrSingle){
        RetAtsForeignCurrPayDetail tmpRetDetail = new RetAtsForeignCurrPayDetail();
        tmpRetDetail.respCode = "-1";
        tmpRetDetail.respInfo = "暂不支持全球汇款接口";
        return tmpRetDetail;
    }

    /**
     * 境内外币汇款状态查询
     * @param reqQryForeignCurrSingle
     * @return
     */
    public RetAtsForeignCurrPayDetail payResultDomesticFcp(ReqQryForeignCurrSingle reqQryForeignCurrSingle){
        //返回ATS的对象
        RetAtsForeignCurrPayDetail tmpRetAtsDetail = new RetAtsForeignCurrPayDetail();
        try{
            tmpRetAtsDetail.respCode = "";
            if(tmpRetAtsDetail.respCode.length() == 0){
                bnkPackageID = reqQryForeignCurrSingle.askNumber;
                bnkTransCode = "QLOCEXCH";
                bnkfSeqno = bnkPackageID;
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入ICBC02通用的头部节点
                writeCommonHeader(xmlWriter);
                //写入报文体
                xmlWriter.writeElementString("QryfSeqno", reqQryForeignCurrSingle.askNumber);//待查指令包序列号
                xmlWriter.writeElementString("QrySerialNo", "");//待查平台交易序列号
                xmlWriter.writeElementString("ReqReserved1", "");//备用字段1
                xmlWriter.writeElementString("ReqReserved2", "");//备用字段2
                xmlWriter.writeStartElement("rd");
                xmlWriter.writeElementString("iSeqno", "1");//指令包顺序号
                xmlWriter.writeElementString("QryiSeqno", "1");//待查指令包顺序号
                xmlWriter.writeElementString("QryOrderNo", "");//待查平台交易顺序号
                xmlWriter.writeElementString("ReqReserved3", "");//备用字段3
                xmlWriter.writeElementString("ReqReserved4", "");//备用字段4
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if(tmpStrRet[0].length() == 0){
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRespCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        if (isActiveRetCode(tmpRespCode)){
                            while (tmpBkReader.read("/CMS/eb/out/rd")){
                                XElement readXElement = tmpBkReader.readedXElement;
                                String result=readXElement.getSingleTextNotNull("Result");//指令状态
                                tmpRetAtsDetail.respCode = "0";
                                tmpRetAtsDetail.respInfo =  tmpRetInfo;
                                tmpRetAtsDetail.payInfoCode = tmpRespCode+"+"+result;
                                tmpRetAtsDetail.transState = tmpRetAtsDetail.payInfoCode;
                                tmpRetAtsDetail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                            }
                        }else{
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = tmpRetInfo.toString();
                        }
                    }else{
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                    }
                }else{
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpStrRet[1];
                }
            }
        }catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        return tmpRetAtsDetail;
    }

    /**
     * 跨境外币汇款状态查询
     * @param reqQryForeignCurrSingle
     * @return
     */
    public RetAtsForeignCurrPayDetail payResultCrossborderFCP(ReqQryForeignCurrSingle reqQryForeignCurrSingle) throws Exception{
        //返回ATS的对象
        RetAtsForeignCurrPayDetail tmpRetAtsDetail = new RetAtsForeignCurrPayDetail();
        Connection conn=FGBiz.getLocalDbConn();
        try{
            tmpRetAtsDetail.respCode = "";
            if(tmpRetAtsDetail.respCode.length() == 0){
                bnkPackageID = reqQryForeignCurrSingle.askNumber;
                bnkTransCode = "QRYKJWHKK";
                bnkfSeqno = bnkPackageID;
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入ICBC02通用的头部节点
                writeCommonHeader(xmlWriter);
                //写入报文体
                String date =reqQryForeignCurrSingle.bgnDate.substring(0,10).replaceAll("-","");
                xmlWriter.writeElementString("BeginDate", date);//起始时间
                xmlWriter.writeElementString("EndDate", date);//结束时间
                xmlWriter.writeElementString("Result", "");//指令状态
                xmlWriter.writeElementString("NextTag", "");//查询分页标识
                Map<String, Object> selMap=JDBCHelper.execQuerySingle(conn,SELECTSEQ,new Object[]{reqQryForeignCurrSingle.askNumber});
                if(selMap.size()>0){
                    xmlWriter.writeElementString("ReqReserved1", selMap.get("RESERVE1").toString());//收款人开户行是否为工行 1是2否
                }else {
                    throw new Exception("流水号"+reqQryForeignCurrSingle.askNumber+"未成功存入收款人开户行是否为工行标志信息");
                }
                xmlWriter.writeElementString("ReqReserved2", "");//备用字段2
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                //测试
                //tmpStrRet[0]="";
                //tmpStrRet[1]="<?xml version=\"1.0\" encoding=\"gb2312\"?><CMS>    <eb>        <pub>            <TransCode>QRYKJWHKK</TransCode>            <CIS>200890000150269</CIS>            <BankCode>102</BankCode>            <ID>cspctest2.d.2008</ID>            <TranDate>20181108</TranDate>            <TranTime>200227557</TranTime>            <fSeqno>1111111111</fSeqno>            <RetCode>0000</RetCode>            <RetMsg>成功</RetMsg>        </pub>        <out>            <BeginDate>20181108</BeginDate>            <EndDate>20181108</EndDate>            <Result></Result>            <NextTag></NextTag>            <RepReserved1></RepReserved1>            <RepReserved2></RepReserved2>                    <rd>                        <QryfSeqno>2222222</QryfSeqno>                        <SerialNo></SerialNo>                        <Result>1</Result>                        <iRetCode>1</iRetCode>                        <iRetMsg></iRetMsg>                    </rd>                    <rd>                        <QryfSeqno>2222222</QryfSeqno>                        <SerialNo></SerialNo>                        <Result>1</Result>                        <iRetCode>1</iRetCode>                        <iRetMsg></iRetMsg>                    </rd>                    <rd>                        <QryfSeqno>1111111111</QryfSeqno>                        <SerialNo></SerialNo>                        <Result>1</Result>                        <iRetCode>1</iRetCode>                        <iRetMsg></iRetMsg>                    </rd>        </out>    </eb></CMS>\n";
                //WriteBankLogLn(tmpStrRet[1]);

                if(tmpStrRet[0].length() == 0){
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRespCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        if (isActiveRetCode(tmpRespCode)){
                            while(tmpBkReader.read("/CMS/eb/out/rd")){
                                XElement readXElement = tmpBkReader.readedXElement;
                                //所查询指令的指令包序列号是否为要查询的指令包序列号
                                if(reqQryForeignCurrSingle.askNumber.equals(readXElement.getSingleTextNotNull("QryfSeqno"))){
                                    String result=readXElement.getSingleTextNotNull("Result");//指令状态
                                    tmpRetAtsDetail.respCode = "0";
                                    tmpRetAtsDetail.respInfo =  tmpRetInfo;
                                    tmpRetAtsDetail.payInfoCode = tmpRespCode+"+"+result;
                                    tmpRetAtsDetail.transState = tmpRetAtsDetail.payInfoCode;
                                    tmpRetAtsDetail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                                }
                            }
                            if(!"0".equals(tmpRetAtsDetail.respCode)){
                                tmpRetAtsDetail.respCode = "0";
                                tmpRetAtsDetail.respInfo =  tmpRetInfo;
                                tmpRetAtsDetail.payInfoCode = tmpRespCode+"+"+"0";//提交成功待银行处理
                                tmpRetAtsDetail.transState = tmpRetAtsDetail.payInfoCode;
                                tmpRetAtsDetail.reqSeqID = reqQryForeignCurrSingle.askNumber;
                            }
                        }else{
                            tmpRetAtsDetail.respCode = "-1";
                            tmpRetAtsDetail.respInfo = tmpRetInfo.toString();
                        }
                    }else{
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                    }
                }else{
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpStrRet[1];
                }
            }
        }catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }finally {
            conn.close();
        }
        return tmpRetAtsDetail;
    }

    /**
     * 全球汇款状态查询
     * @param reqQryForeignCurrSingle
     * @return
     */
    public RetAtsForeignCurrPayDetail payResultOutsideFcp(ReqQryForeignCurrSingle reqQryForeignCurrSingle){
        //返回ATS的对象
        RetAtsForeignCurrPayDetail tmpRetAtsDetail = new RetAtsForeignCurrPayDetail();
        tmpRetAtsDetail.respCode = "-1";
        tmpRetAtsDetail.respInfo = "暂不支持全球汇款状态查询接口";
        return tmpRetAtsDetail;
    }
}
